﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using University.Workshop.DataAccess;
using University.Workshop.Business;
using University.Workshop.Model;
using University.Workshop.Presenters.AppointmentFolder;
using University.Workshop.WinApp.CashManagementFolder;

namespace University.Workshop.WinApp
{
    public partial class SearchAppointmentPayment : Form, ISearchAppointment
    {
        int indexId;
        List<Appointment> appointment;
        PresenterSearchAppointment presenter;
        CashManagementForm father;

        public SearchAppointmentPayment()
        {
            InitializeComponent();
            presenter = new PresenterSearchAppointment(this);
        }

        public SearchAppointmentPayment(CashManagementForm father)
        {
            InitializeComponent();
            presenter = new PresenterSearchAppointment(this);

            this.father = father;
        }

        private void SearchAppointment_Load(object sender, EventArgs e)
        {
            indexId = -1;
            //appointment = presenter.SearchAppointment();
            dgwBuscar.DataSource = null;
        }

        private void btnBuscar_Click(object sender, EventArgs e)
        {
            string PatientId = String.Empty;
            //DataTable dtb = new DataTable();
            
            try
            {
                try
                {
                    //this.dgwBuscar.DataSource = dtb;
                    PatientId = txtPatientId.Text;

                    appointment = (List<Appointment>)presenter.SearchAppointment();

                    dgwBuscar.DataSource = appointment;

                    //labelInfo.Text = String.Format("Cantidad de registros encontrados: {0}", dtb.Rows.Count);
                }
                catch (Exception ex)
                {
                    labelInfo.Text = "Error: " + ex.Message;
                }
            }

            catch (Exception ex)
            {
                MessageBox.Show("ERROR: " + ex.Message, " Comprobar tabla");
            }
        }

        private void btnSeleccionar_Click(object sender, EventArgs e)
        {
            try
            {
                if (indexId != -1)
                {
                    //labelInfo.Text = dgwBuscar.CurrentRow.Cells["PatientId"].Value.ToString();

                    father.PatientId = dgwBuscar.CurrentRow.Cells["PatientId"].Value.ToString();
                    father.DoctorId = dgwBuscar.CurrentRow.Cells["DoctorId"].Value.ToString();

                    father.AppointmentId = dgwBuscar.CurrentRow.Cells["AppointmentId"].Value.ToString();

                    father.DateAppointment = dgwBuscar.CurrentRow.Cells["Date"].Value.ToString();
                    

                    this.Dispose();
                    /*PatientHistory patienthistory = new PatientHistory(label2.Text);
                    this.Visible = false;
                    if (patienthistory.ShowDialog() == DialogResult.Cancel)
                        this.Visible = true;
                    indexId = -1;
                    criterio.Clear();
                    DataPatient.DataSource = null;*/
                }
                else
                {
                    MessageBox.Show("Seleccione un paciente");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }



        string ISearchAppointment.PatientId
        {
            get
            {
                return txtPatientId.Text;
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        private void btnCancelar_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void dgwBuscar_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            indexId = e.RowIndex;
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void txtPatientId_TextChanged(object sender, EventArgs e)
        {

        }

        private void dgwBuscar_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void labelInfo_Click(object sender, EventArgs e)
        {

        }
    }
}
